package com.qipay.sp.impl;

import com.qipay.baas.model.SpChannel;
import com.qipay.mapper.SpChannelMapper;
import com.qipay.scalar.type.ChannelTypeEnum;
import com.qipay.sp.SpChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;

@Service
public class SpChannelServiceImpl implements SpChannelService {

    @Autowired
    SpChannelMapper spChannelMapper;
//
//    @Override
    @Transactional
    protected SpChannel next(ChannelTypeEnum channelTypeEnum) {
        SpChannel spChannel = spChannelMapper.selectByChannelTypeAndStatus
                (channelTypeEnum.getType(), (byte) 0);
        Assert.notNull(spChannel, "找不到可用的支付通道");
        spChannel.setUpdateDate(new Date());
        spChannelMapper.updateByPrimaryKey(spChannel);
        return spChannel;
    }

    @Override
    @Transactional
    public SpChannel next(ChannelTypeEnum channelTypeEnum, Integer orderAmount) {
        SpChannel spChannel = spChannelMapper.selectByChannelTypeAndStatusAndRule
                (channelTypeEnum.getType(), orderAmount);
        Assert.notNull(spChannel, "找不到可用的支付通道");
        spChannel.setUpdateDate(new Date());
        spChannelMapper.updateByPrimaryKey(spChannel);
        return spChannel;
    }

    @Override
    public SpChannel next(ChannelTypeEnum channelTypeEnum, Integer orderAmount,List<Long>spIds) {
        SpChannel spChannel = spChannelMapper.selectByChannelTypeAndStatusAndRuleAndSpId
                (channelTypeEnum.getType(), orderAmount,spIds);
        Assert.notNull(spChannel, "找不到可用的支付通道");
        spChannel.setUpdateDate(new Date());
        spChannelMapper.updateByPrimaryKey(spChannel);
        return spChannel;
    }

    @Override
    @Transactional
    public int insert(SpChannel sp) {
        return spChannelMapper.insert(sp);
    }

    @Override
    @Transactional
    public int update(SpChannel sp) {
        return spChannelMapper.updateByPrimaryKey(sp);
    }

    @Override
    @Transactional
    public int updateState(int id, byte state) {
        SpChannel spChannel = new SpChannel();
        spChannel.setId(id);
        spChannel.setState(state);
        return spChannelMapper.updateByPrimaryKeySelective(spChannel);
    }

    @Override
    @Transactional
    public int delete(int id) {
        return spChannelMapper.deleteByPrimaryKey(id);
    }

    @Override
    public SpChannel get(int id) {
        return getOneById(id);
    }

    @Override
    public List<SpChannel> all() {
        return spChannelMapper.all();
    }

    @Override
    public List<SpChannel> selectBySpId(int id) {
        return spChannelMapper.selectBySpId(id);
    }

    @Override
    public SpChannel getOneById(Integer id) {
        return spChannelMapper.selectByPrimaryKey(id);
    }

    @Override
    public SpChannel getOneBySpIdAndChannelType(Long spid, ChannelTypeEnum channelTypeEnum){
        return spChannelMapper.selectByChannelTypeAndSpid(channelTypeEnum.getType(), spid);
    }

}
